package com.ruoyi.mhrb.platform.controller;

import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mhrb.platform.domain.MhrbCustomer;
import com.ruoyi.mhrb.platform.service.IMhrbCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客服配置Controller
 *
 * @author mhrb
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/mhrb/platform/customer")
public class MhrbCustomerController extends BaseController {
    @Autowired
    private IMhrbCustomerService mhrbCustomerService;

    /**
     * 查询客服配置列表
     */
    @PreAuthorize("@ss.hasPermi('mhrb:platform:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(MhrbCustomer mhrbCustomer) {
        startPage();
        List<MhrbCustomer> list = mhrbCustomerService.selectMhrbCustomerList(mhrbCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客服配置列表
     */
    @PreAuthorize("@ss.hasPermi('mhrb:platform:customer:export')")
    @Log(title = "客服配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MhrbCustomer mhrbCustomer) {
        List<MhrbCustomer> list = mhrbCustomerService.selectMhrbCustomerList(mhrbCustomer);
        ExcelUtil<MhrbCustomer> util = new ExcelUtil<MhrbCustomer>(MhrbCustomer.class);
        util.exportExcel(response, list, "客服配置数据");
    }

    /**
     * 获取客服配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('mhrb:platform:customer:query')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId) {
        return success(mhrbCustomerService.selectMhrbCustomerByCustomerId(customerId));
    }

    /**
     * 新增客服配置
     */
    @PreAuthorize("@ss.hasPermi('mhrb:platform:customer:add')")
    @Log(title = "客服配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MhrbCustomer mhrbCustomer) {
        return toAjax(mhrbCustomerService.insertMhrbCustomer(mhrbCustomer));
    }

    /**
     * 修改客服配置
     */
    @PreAuthorize("@ss.hasPermi('mhrb:platform:customer:edit')")
    @Log(title = "客服配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MhrbCustomer mhrbCustomer) {
        return toAjax(mhrbCustomerService.updateMhrbCustomer(mhrbCustomer));
    }

    /**
     * 删除客服配置
     */
    @PreAuthorize("@ss.hasPermi('mhrb:platform:customer:remove')")
    @Log(title = "客服配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds) {
        return toAjax(mhrbCustomerService.deleteMhrbCustomerByCustomerIds(customerIds));
    }

    /**
     * 随机客服
     */
    @PreAuthorize("@ss.hasPermi('mhrb:platform:customer:list')")
    @GetMapping("/randomOne")
    public AjaxResult randomOne() {
        MhrbCustomer mhrbCustomer = new MhrbCustomer();
        mhrbCustomer.setStatus(1L);
        List<MhrbCustomer> list = mhrbCustomerService.selectMhrbCustomerList(mhrbCustomer);
        if (CollectionUtils.isEmpty(list)) {
            return error("没有客服");
        }
        Random random = new Random();
        // 从列表中随机选择一个元素
        int randomIndex = random.nextInt(list.size());
        return AjaxResult.success(list.get(randomIndex));
    }


}
